package cn.txy.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.txy.constants.UserType;
import cn.txy.mapper.DriverSettingMapper;
import cn.txy.mapper.DriverSummaryMapper;
import cn.txy.mapper.DriverWalletMapper;
import cn.txy.openfeign.UaaOpenFeign;
import cn.txy.pojo.domain.*;
import cn.txy.mapper.DriverMapper;
import cn.txy.result.JSONResult;
import cn.txy.service.IDriverService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 * 司机对象 服务实现类
 * </p>
 *
 * @author txy
 * @since 2025-03-25
 */
@Service
public class DriverServiceImpl extends ServiceImpl<DriverMapper, Driver> implements IDriverService {
    @Autowired
    private DriverWalletMapper driverWalletMapper;
    @Autowired
    private DriverMapper driverMapper;
    @Autowired
    private DriverSettingMapper driverSettingMapper;
    @Autowired
    private DriverSummaryMapper driverSummaryMapper;
    @Autowired
    private UaaOpenFeign uaaOpenFeign;

    @Override
    @Transactional
    public JSONResult register(Driver driver) {
        //不建议用这种因为容易对不上名字
//        QueryWrapper<Driver> driverQueryWrapper = new QueryWrapper<>();
//        driverQueryWrapper.eq("open_id",driver.getOpenId());

        Driver driver2 = driverMapper.selectOne(new LambdaQueryWrapper<Driver>().
                eq(Driver::getOpenId,driver.getOpenId()));
        if (driver2 != null) {
            return JSONResult.success();
        } else {
            BigDecimal bigDecimal = new BigDecimal(0);
            Date date = new Date();
            Driver driver1 = new Driver();
            driver1.setOpenId(driver.getOpenId());
            driver1.setPhone(driver.getPhone());
            driver1.setCreateTime(date);
            long id = IdUtil.createSnowflake(0L, 0L).nextId();
            driver1.setId(id);
            super.save(driver1);
            //-------------------------------钱包----------------------------------
            DriverWallet driverWallet = new DriverWallet();
            driverWallet.setId(id);
            driverWallet.setAmount(bigDecimal);
            driverWallet.setPassword("123456");
            driverWallet.setDeleted(false);
            driverWallet.setCreateTime(date);
            driverWallet.setTotalRecharge(bigDecimal);
            driverWallet.setTotalWithdraw(bigDecimal);
            driverWalletMapper.insert(driverWallet);
            //-----------------------------设置对象--------------------------------
            DriverSetting driverSetting = new DriverSetting();
            driverSetting.setId(id);
            driverSetting.setAutoAccept(true);
            driverSetting.setOrientation(true);
            driverSetting.setListenService(true);
            driverSetting.setOrderDistance(50);
            driverSetting.setRangeDistance(50);
            driverSetting.setCreateTime(date);
            driverSetting.setDeleted(false);
            driverSetting.setVersion(0);
            driverSettingMapper.insert(driverSetting);
            //-------------------------------结算对象---------------------------
            DriverSummary driverSummary = new DriverSummary();
            driverSummary.setId(id);
            driverSummary.setLevel(0);
            driverSummary.setTotalOrders(0);
            driverSummary.setWeekOrders(0);
            driverSummary.setGoodComments(0);
            driverSummary.setAppeal(0);
            driverSummary.setTotalComplaint(0);
            driverSummary.setTotalCancel(0);
            driverSummary.setDriveDuration(0);
            driverSummary.setTradeOrders(0);
            driverSummary.setTodayTradeOrders(0);
            driverSummary.setTodayComplaint(0);
            driverSummary.setTotalCancel(0);
            driverSummary.setTodayIncome(bigDecimal);
            driverSummary.setCreateTime(date);
            driverSummary.setDeleted(false);
            driverSummary.setVersion(0);
            driverSummaryMapper.insert(driverSummary);
            //-------------------------------openfeign远程调用---------------------------
            LoginVo loginVo = new LoginVo();
            loginVo.setId(id);
            loginVo.setOpenId(driver.getOpenId());
            loginVo.setPhone(driver.getPhone());
            loginVo.setType(UserType.DRIVER);
            uaaOpenFeign.register(loginVo);
        }
        return JSONResult.success();
    }
}
